/*
 * MyTableModel.java
 *
 * Created on 2 de octubre de 2005, 04:04 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ig.util;

//import com.hxtt.swing.util.TableSorter;
import ig.metrica.riesgo.FactorRiesgoMatrizCorrelacionMetrica;
import javax.swing.table.AbstractTableModel;
//import javax.swing.table.JTableHeader;

public /*class MyTableModel extends TableSorter{
    private MyTableModel1 myTableModel1=null;
    public MyTableModel(Object[][] object,String[] columnNames,boolean[] editables){
        myTableModel1=new MyTableModel1(object,columnNames,editables);        
        super.setTableModel(myTableModel1);
    }

    public MyTableModel setHeader(JTableHeader heder){
        super.setTableHeader(heder);
        return this;
    }
    public boolean isDelFisico() {
        return myTableModel1.isDelFisico();
    }
    
    public String[] getEstado() {
        return myTableModel1.getEstado();
    }
    
    public void setData(Object[][] _data){
        myTableModel1.setData(_data);
    }
    
    public Object[][] getData() {
        return myTableModel1.getData();
    }
    public Object[][] getObjet() {
        return myTableModel1.getObjet();
    }
}
*/
class MyTableModel extends AbstractTableModel{
    private boolean     DEBUG=false;
    private boolean[]   editables=null;
    private String[]    estado=null;
    private String[]    estadoAnterior=null;
    private Object[][]  object;
    private String[] columnNames = null;
    private Object[][] data =null;
    private boolean delFisico=false;

    public MyTableModel(Object[][] object,String[] columnNames,boolean[] editables){
        this.editables=editables;
        this.columnNames=columnNames;
        if(object!=null)
            this.object=object;
        else
            this.object= new Object[0][columnNames.length];
        setData(this.object);
    }
    
    public boolean isDelFisico() {
        return delFisico;
    }
    
    public String[] getEstado() {
        return estado;
    }
    
    public void setData(Object[][] _data){
        if(_data!=null){
            data=new Object[_data.length][columnNames.length];
            for (int j = 0; j <columnNames.length; j++) {
                for (int i = 0; i < _data.length; i++) {
                    this.data[i][j] = _data[i][j];
                }
            }
            fireTableDataChanged();
        }
    }
    
    
    private void copyEstadosNuevos(){
        estadoAnterior = new String[estado.length];
        for (int i = 0; i < estado.length; i++) {
            estadoAnterior[i]=estado[i];
        }
    }
    
    private void copyEstadosViejos(){
        for (int i = 0; i < estadoAnterior.length; i++){
            estado[i]=estadoAnterior[i];
        }
        copyEstadosNuevos();
    }
    
    
    //super.addTableModelListener()
    public int getColumnCount() {
        return columnNames.length;
    }
    
    public int getRowCount() {
        return data.length;
    }
    
    public String getColumnName(int col) {
        return columnNames[col];
    }
    
    public synchronized Object getValueAt(int row, int col) {
        //System.out.println("row - col:"+row+"-"+col);
        try {
            return data[row][col];
        } catch (Exception e) {
            return "";
        }
    }
    
    public Class getColumnClass(int col) {
        Class  ret = getValueAt(0, col).getClass();
        return ret;
    }
    
    public boolean isCellEditable(int row, int col) {
        if (editables[col]==true)
            return true;
        else
            return false;
        
    }
    
    public Object[][] getData() {
        return this.data;
    }
    public Object[][] getObjet() {
        return this.object;
    }
    public void setValueAt(Object value, int row, int col){
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                    + " to " + value
                    + " (an instance of "
                    + value.getClass() + ")");
        }
        if(data[row][col] instanceof FactorRiesgoMatrizCorrelacionMetrica)
            ((FactorRiesgoMatrizCorrelacionMetrica)data[row][col]).setNombre(value.toString());
        else
            data[row][col] = value;
        if (DEBUG){
            System.out.println("New value of data:");
            printDebugData();
        }
        setData(data);
        
    }
    
    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();
        
        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
    }

    public void setEditables(int i,boolean value) {
        this.editables[i] = value;
        fireTableDataChanged();
    }
    
    
}
